# -*- coding: utf-8 -*-
# -*- author: GXR -*-
import requests
import pymysql

db = pymysql.Connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='1111',
    db='rikao',
    charset='utf8'
)
cur = db.cursor()

for tab in range(1, 4):
    for page in range(1, 16):
        url = 'http://www.szse.cn/api/report/ShowReport/data?&CATALOGID=1110&TABKEY=tab' + str(tab) + '&PAGENO=' + str(
            page)
        response = requests.get(url=url)
        if tab == 1:
            table = response.json()[0]
        elif tab == 2:
            table = response.json()[1]
        else:
            table = response.json()[2]
        data = table['data']
        for gp in data:
            zqdm = gp['zqdm']
            gsjc = gp['gsjc']
            print(zqdm, gsjc)
            sql = "insert gupiao values(0,'%s','%s')" % (zqdm, gsjc)
            cur.execute(sql)
            db.commit()
cur.close()
db.close()
